package com.zjj.mybatis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zjj.mybatis.bean.User;
import com.zjj.mybatis.mapper.UserMapper;

@RestController
public class UserController {

	@Autowired
	private UserMapper userService;
	
	@GetMapping("/user")
	public List<User> userList() {
		return userService.queryUserList();
	}
	
	@GetMapping("/user/{id}")
	public User userInfo(@PathVariable Integer id) {
		return userService.queryUserById(id);
	}
	
	@PostMapping("/user")
	public boolean addUser(@RequestParam String name, @RequestParam Integer age) {
		User user = new User();
		user.setAge(age);
		user.setName(name);
		return userService.addUser(user);
	}
	
	@PutMapping("/user/{id}")
	public boolean updateUser(@PathVariable Integer id, @RequestParam String name, @RequestParam Integer age) {
		User user = new User();
		user.setAge(age);
		user.setName(name);
		user.setId(id);
		return userService.updateUser(user);
	}
	
	@DeleteMapping("/user/{id}")
	public boolean deleteUser(@PathVariable Integer id) {
		return userService.deleteUser(id);
	}
}
